<!--给用户名做AJAX判断-->
$(function () {
    //定义正则表达式
    //根据用户注册的表达式
    let regExpByUname = /^[a-zA-Z]{6,16}.*$/;
    let regExpByPassword = /^[a-zA-z\d]\w{7,16}.*$/;
//根据手机号注册的部分
    let regExpByPhone = /^[\d]{11}/;
    //做ajax的同时做校验
    $("#phone").unbind("blur").bind("blur", function () {
        //获得用户名
        //    通过ajax传到服务器判断用户是否存在
        let uname = $("#phone").val().toString();
        if (uname == null || uname === '') {
            $("#ajaxWithDown").text("手机号不能为空").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", true);
        } else if (!regExpByPhone.test(uname)) {
            $("#ajaxWithDown").text("手机号长度必须是11位并且必须是数字").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", true);
        } else {
            $.get("http://localhost:8080/Shop_war_exploded/users/ajax/?uname=" + uname, function (result) {
                if (result >= 1) {
                    $("#ajaxWithDown").text("手机已经注册").css("color", "red").css("textFamily", "宋体");
                    $("#registry2").attr("disabled", true);
                } else {
                    $("#ajaxWithDown").text("手机号可以注册").css("color", "red").css("textFamily", "宋体");
                    $("#registry2").attr("disabled", false);
                }
            })
        }
    });

    $("#passwordWithPhone").on("blur", function () {
        let password = $("#passwordWithPhone").val().toString();
        if (password === null || password === "") {
            $("#ajaxWithDown").text("密码不能为空").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", true);
        } else if (!regExpByPassword.test(password)) {
            $("#ajaxWithDown").text("密码最少7位最多16位").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", true);
        } else {
            $("#ajaxWithDown").text("手机号可以注册").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", false);
        }
    })

    $("#passwordRepeatWithPhone").on("blur", function () {
        let password = $("#passwordWithPhone").val().toString();
        let passwordRepeat = $("#passwordRepeatWithPhone").val().toString();
        if (password !== passwordRepeat) {
            $("#ajaxWithDown").text("两次密码不一致").css("color", "red").css("textFamily", "宋体");
            $("#registry2").attr("disabled", false);
        }
    })

    //做ajax的同时做校验
    //根据用户名部分
    $("#uname").unbind("blur").bind("blur", function () {
        //获得用户名
        let uname = $("#uname").val().toString();
        //    通过ajax传到服务器判断用户是否存在
        if (uname == null || uname === "") {
            $("#ajaxWithUp").html("用户名不能为空").css("color", "red").css("textFamily", "宋体");
        } else if (!regExpByUname.test(uname)) {
            $("#ajaxWithUp").html("用户名最少6位最多16位并且不能有中文").css("color", "red").css("textFamily", "宋体");
        } else {
            $.get("http://localhost:8080/Shop_war_exploded/users/ajax/?uname=" + uname, function (result) {
                if (result >= 1) {
                    $("#ajaxWithUp").html("用户名已经存在").css("color", "red").css("textFamily", "宋体");
                    $("#registry1").attr("disabled", true);
                } else {
                    $("#ajaxWithUp").html("用户名可以使用").css("color", "red").css("textFamily", "宋体");
                    $("#registry1").attr("disabled", false);
                }
            })
        }
    });

    $("#password").bind("blur", function () {
        let upwd = $("#password").val().toString();
        if (!regExpByPassword.test(upwd)) {
            $("#ajaxWithUp").html("密码最少7位最多16位").css("color", "red").css("textFamily", "宋体");
            $("#registry1").attr("disabled", true);
        } else if (upwd == null || upwd === "") {
            $("#ajaxWithUp").text("密码不能为空").css("color", "red").css("textFamily", "宋体");
            $("#registry1").attr("disabled", true);
        } else {
            $("#ajaxWithUp").html("用户名可以使用").css("color", "red").css("textFamily", "宋体");
            $("#registry1").attr("disabled", false);
        }
    })

    $("#passwordRepeat").on("blur", function () {
        let repeatPassword = $("#passwordRepeat").val().toString();
        let password = $("#password").val().toString();
        if (repeatPassword !== password) {
            $("#ajaxWithUp").html("两次密码不一致").css("color", "red").css("textFamily", "宋体");
            $("#registry1").attr("disabled", true);
        }
    })

})